package com.rentcars.security;

import com.rentcars.entity.Account;
import com.rentcars.mapper.AccountMapper;
import com.rentcars.utils.Md5Util;
import javax.annotation.Resource;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * 类描述：
 *
 * @author 张吉哲
 * @date 2021/3/2 14:41
 */
public class BosCredentialsMatcher extends SimpleCredentialsMatcher {

  @Resource
  private AccountMapper accountMapper;

  @Override
  public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
    String username = new String(usernamePasswordToken.getUsername());
    Account account = accountMapper.selectAccountByUsername(username);
    if (account == null) {
      return false;
    }
    String userpwd = new String(usernamePasswordToken.getPassword());
    String newPwd = Md5Util.md5(userpwd + account.getPwdsalt());
    return newPwd.equals(account.getUserpwd());
  }
}
